/**
 * 分页对象 - 根据参数生成分页信息
 * hanson
 */
define(['public/js/util'], function(Util){
	'use strict';
	
	var P = {};
	
	var Config = {
			// 数据页码信息
			page_info: {
				current_page: 0, // 当前页
				page_rows: 0, // 每页记录数
				total_pages: 0, // 总页数
				total_rows: 0 //  总记录数
			},
			// 分页自定义样式
			class_config: {
				page_wrap_c:'page-wrap',
				page_word_wrap_c:'page-word-wrap',
				page_word_number_inp_c:'page-word-number-inp',
				page_word_skip_btn_c:'page-word-skip-btn',
				page_number_wrap_c:'page-number-wrap',
				page_first_c:'page-first',
				page_previous_c:'page-previous',
				page_number_c:'page-number',
				page_current_c:'page-current',
				page_next_c:'page-next',
				page_last_c:'page-last'
			},
			// 分页节点
			page_node: {
				$dom:null,
			},
			/**
			 * 点击页码事件, 只有当配置了 page_node的 $dom之后才会生效
			 */
			success: function(){
				
			}
	}
	
	/**
	 * 获取分页模板
	 * @param currentPage 当前页
	 * @param pageRows   每页记录数
	 * @param totalPages 总页数
	 * @param totalRows  总记录数
	 */
	P.paging = function(config){
		var cfx = $.extend(true, Config, config);
		var paging_html = '';
		if(cfx.page_info.total_pages > 0){
			var page = getPagingParameter(cfx.page_info.current_page, cfx.page_info.page_rows, cfx.page_info.total_pages, cfx.page_info.total_rows);
			paging_html = getPagingTemplateHtml(cfx.class_config, page);
		}
		if(cfx.page_node.$dom == null){
			return paging_html;
		}else{
			var $dom = cfx.page_node.$dom;
			var cc = cfx.class_config;
			$dom.html(paging_html);
			if(Util.isFun(cfx.success)){
				$dom.find('ul.'+cc.page_number_wrap_c+' li').click(function(){
					var number = $(this).find('a').data('number');
					var page_rows = $dom.find('.inp_page_rows').val();
					cfx.success({
						number: number,
						page_rows: page_rows
					});
				});
				$dom.find('.btn_skip_number').click(function(){
					var number = $dom.find('.inp_skip_number').val();
					var page_rows = $dom.find('.inp_page_rows').val();
					cfx.success({
						number: number,
						page_rows: page_rows
					});
				});
			}
		}
	}
	
	// 获取分页需要用到的参数    以便于自定义构造html
	function getPagingParameter(currentPage, pageRows, totalPages, totalRows){
		if(totalPages > 0){
			/* 构造html页码 */
			var page_previous = (currentPage-1) >= 1 ? (currentPage-1) : currentPage; // 上一页
			var page_next = (currentPage+1) <= totalPages ? (currentPage+1) : totalPages; // 下一页
			var page_begin =  (currentPage-3) >= 1 ? (currentPage-3) : 1; // 第一个页数
			var page_end = (currentPage+3) <= totalPages ? (currentPage+3) : totalPages;  // 最后一个页数
			var page_first = 1;  // 首页
			var page_last = totalPages;  // 尾页
			var page_current = currentPage; // 当前页
			var page_total_number = totalPages; // 总共的页数
			var page_total_rows = totalRows; // 总共的记录数
			var page_rows = Util.isEmpty(pageRows) ? 10 : pageRows; // 每页显示记录数，默认显示10条
			var page = {
					page_previous:page_previous,
					page_next:page_next,
					page_begin:page_begin,
					page_end:page_end,
					page_first:page_first,
					page_last:page_last,
					page_current:page_current,
					page_total_number:page_total_number,
					page_total_rows:page_total_rows,
					page_rows:page_rows
			}
			return page;
		}
		return null;
	}
	
	
	// 获取分页的   页码html模板
	function getPagingTemplateHtml(config, page){
		// 生成分页模板
		var paging_html = 
			'<div class="'+config.page_wrap_c+'">'+
				'<div class="'+config.page_word_wrap_c+'">'+
					function(){
						if(Util.isEmpty(page.page_total_rows)){
							return '<span>共'+page.page_total_rows+'条记录，'+page.page_total_number+'页；</span>';
						}else{
							return '<span>共'+page.page_total_number+'页；</span>';
						}
					}()+
					'<span>显示<input type="text" value="'+page.page_rows+'" class="inp_page_rows '+config.page_word_number_inp_c+'"/>条/页，'+
						'跳到第<input type="text" value="'+page.page_current+'" class="inp_skip_number '+config.page_word_number_inp_c+'"/>页'+
						'<button type="button" class="btn_skip_number '+config.page_word_skip_btn_c+'">确定</button>'+
					'</span>'+
			   '</div>'+
			   '<ul class="'+config.page_number_wrap_c+'">'+
					'<li class="'+config.page_first_c+'"><a data-number="'+page.page_first+'">首页</a></li>'+
					'<li class="'+config.page_previous_c+'"><a data-number="'+page.page_previous+'">上一页</a></li>'+
					function(){
							var html_number = '';
							for(var i=page.page_begin; i<=page.page_end; i++){
								if(i==page.page_current){
									html_number += '<li class="'+config.page_number_c+' '+config.page_current_c+'"><a data-number="'+i+'">'+i+'</a></li>';
								}else{
									html_number += '<li class="'+config.page_number_c+'"><a data-number="'+i+'">'+i+'</a></li>';
								}
							}
							return html_number;
					}()+
					'<li class="'+config.page_next_c+'"><a data-number="'+page.page_next+'">下一页</a></li>'+
					'<li class="'+config.page_last_c+'"><a data-number="'+page.page_last+'">尾页</a></li>'+
				'</ul>'+
			'</div>';
		return paging_html;
	}
	
	
	return P;
});